package com.base.pig.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.pig.dto.AlarmListDto;
import com.base.pig.dto.AlarmNowDto;
import com.base.pig.dto.AlarmOnTimeDto;
import com.base.pig.dto.TableDto;
import com.base.pig.entity.SensorCheckAlarm;
import com.base.pig.mapper.SensorCheckAlarmMapper;
import com.base.pig.service.SensorCheckAlarmService;
import com.base.pig.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 程序根据用户设定的上下限判断的报警数据 服务实现类
 * </p>
 *
 * @author wzx
 * @since 2022-04-12
 */
@Service
public class SensorCheckAlarmServiceImpl extends ServiceImpl<SensorCheckAlarmMapper, SensorCheckAlarm> implements SensorCheckAlarmService {

    @Autowired
    private SensorCheckAlarmMapper sensorCheckAlarmMapper;

    @Override
    public IPage<AlarmListVo> queryAlarmList(AlarmListDto alarmListDto){
        IPage<AlarmListVo> page = new Page<>(alarmListDto.getCurrent(), alarmListDto.getSize());
        List<AlarmListVo> vo = sensorCheckAlarmMapper.queryAlarmList(page,alarmListDto);
        return page.setRecords(vo);
    }
    @Override
    public List<AlarmListVo> exportAlarmList(AlarmListDto alarmListDto){
        List<AlarmListVo> vo = sensorCheckAlarmMapper.exportAlarmList(alarmListDto);
        return vo;
    }
    @Override
    public List<AlarmOnTimeVo> alarmNowList(AlarmOnTimeDto alarmOnTimeDto){
        List<AlarmOnTimeVo> vo = sensorCheckAlarmMapper.alarmNowList(alarmOnTimeDto);
        return vo;
    }
    @Override
    public AlarmListVo alarmNowById(AlarmNowDto alarmNowDto){
        AlarmListVo vo = sensorCheckAlarmMapper.alarmNowById(alarmNowDto);
        return vo;
    }

    @Override
    public AlarmListVo alarmByDeviceAndSensor(AlarmNowDto alarmNowDto) {

        List<AlarmListVo>    list= sensorCheckAlarmMapper.alarmByDeviceAndSensor(alarmNowDto);

        if(list!=null && list.size()>0 ){
            return list.get(0);
        }else{
            return new AlarmListVo();
        }

    }

    @Override
    public StatisticResultVo deviceStastistic(TableDto tableDto) {
        StatisticResultVo vo = new StatisticResultVo();
        BigDecimal offlineSum = sensorCheckAlarmMapper.offlineDevice(tableDto);
        BigDecimal sum = sensorCheckAlarmMapper.allDevice(tableDto);
        List<StatisticVo> list = sensorCheckAlarmMapper.typeDevice(tableDto);
        List<StatisticVo> offlinelist = sensorCheckAlarmMapper.offlineTypeDevice(tableDto);
        Map<String, StatisticOnlineVo> onlineMap = new HashMap<String, StatisticOnlineVo>();
        Map<String, String> myMap = new HashMap<String, String>();
        myMap.put("排水液位监测仪","ywdata");
        myMap.put("供水流量监测仪","gdlldata");
        myMap.put("井盖状态监测仪","jgjcdata");
        myMap.put("排水流量监测仪","lljdata");
        myMap.put("供水水质监测仪","szdata");
        myMap.put("一杆式内涝监测站","nldata");
        StatisticOnlineVo nodevo = new StatisticOnlineVo();
        nodevo.setTotal(new BigDecimal("0"));
        nodevo.setOnline(new BigDecimal("0"));
        onlineMap.put("ywdata",nodevo);
        onlineMap.put("gdlldata",nodevo);
        onlineMap.put("jgjcdata",nodevo);
        onlineMap.put("lljdata",nodevo);
        onlineMap.put("szdata",nodevo);
        onlineMap.put("nldata",nodevo);


        for(StatisticVo node:list){
            StatisticOnlineVo onlineVo = new StatisticOnlineVo();
            //onlineVo.setName(node.getName());
            onlineVo.setTotal(node.getValue());
            onlineVo.setOnline(node.getValue());
            for(StatisticVo offnode:offlinelist){
                if (offnode.getName().equals(node.getName())){
                    onlineVo.setOnline(node.getValue().subtract(offnode.getValue()));
                }
            }
            if(myMap.get(node.getName()) != null){
                onlineMap.put(myMap.get(node.getName()),onlineVo);
            }
        }
        vo.setOfflineSum(offlineSum);
        vo.setOnlineSum(sum.subtract(offlineSum));
        vo.setSum(sum);
        vo.setList(list);
        vo.setOnlineMap(onlineMap);
        return vo;
    }

}
